﻿using Manon.Admin.Web.Application.Dtos.Other;
using Manon.Admin.Web.Domain.Entities.Other;
using Manon.Admin.Web.Domain.Repositories;
using Manon.Core;
using Manon.Core.Commons;
using Manon.Core.Extensions;
using Manon.Repository;
using Manon.Repository.Collections;

namespace Manon.Admin.Web.Application.Impl
{
    public class OtherService : IOtherService
    {

        private readonly ILogger<NewsService> _logger;
        private readonly IUnitOfWork _unitOfWork;
        private readonly IApplicationUser _applicationUser;

        private readonly IFriendlyLinkRepository _friendlyLinkRepository;

        public OtherService(ILogger<NewsService> logger,
            IUnitOfWork unitOfWork,
            IApplicationUser applicationUser,
            IFriendlyLinkRepository friendlyLinkRepository)
        {
            _logger = logger;
            _unitOfWork = unitOfWork;
            _applicationUser = applicationUser;
            _friendlyLinkRepository = friendlyLinkRepository;
        }


        /// <summary>
        /// 获取友情链接
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<GetFriendlylinkOutput> GetFriendlylink(int id)
        {
            if (id <= 0) return new GetFriendlylinkOutput() { Enabled = true };

            try
            {

                var result = await _friendlyLinkRepository.GetFirstOrDefaultAsync(x => x.Id == id);

                return result.ConvertTo<FriendlyLink, GetFriendlylinkOutput>();
            }
            catch (Exception ex)
            {
                _logger.LogError("获取友情链接异常", ex);
                throw;
            }
        }

        /// <summary>
        /// 获取友情链接列表
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<PagedList<GetFriendlylinkOutput>> GetFriendlylinkPagedList(GetFriendlylinkPagedListInput Input)
        {
            try
            {
                var result = await _friendlyLinkRepository
                    .GetQuery()
                    .WhereIf(!string.IsNullOrEmpty(Input.Keyword), e => e.Name.Contains(Input.Keyword) || e.Remark.Contains(Input.Keyword))
                    .WhereIf(Input.Enabled.HasValue, e => e.Enabled == Input.Enabled.Value)
                    .ToPagedListAsync<GetFriendlylinkOutput, FriendlyLink>(Input);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("获取友情链接列表异常", ex);
                throw;
            }
        }

        /// <summary>
        /// 保存友情链接 
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<Result> SaveFriendlylink(SaveFriendlylinkInput Input)
        {
            try
            {
                var result = await _friendlyLinkRepository.GetFirstOrDefaultAsync(x => x.Id == Input.Id);
                if (result == null)
                {
                    result = new FriendlyLink();
                    result.ImageUrl = Input.ImageUrl;
                    result.Name = Input.Name;
                    result.OrderIndex = Input.OrderIndex;
                    result.Remark = Input.Remark;
                    result.Url = Input.Url;
                    result.Enabled = Input.Enabled;
                    result.CreateTime = DateTime.Now;
                    result.UpdateTime = DateTime.Now;
                    result.OrderIndex = Input.OrderIndex;
                    result.CreateBy = _applicationUser.UserId;
                    result.UpdateBy = _applicationUser.UserId;
                    await _friendlyLinkRepository.InsertAsync(result);
                }
                else
                {
                    result.ImageUrl = Input.ImageUrl;
                    result.Name = Input.Name;
                    result.OrderIndex = Input.OrderIndex;
                    result.Remark = Input.Remark;
                    result.Url = Input.Url;
                    result.Enabled = Input.Enabled;
                    result.UpdateTime = DateTime.Now;
                    result.OrderIndex = Input.OrderIndex;
                    result.UpdateBy = _applicationUser.UserId;
                    await _friendlyLinkRepository.UpdateAsync(result);
                }
                await _unitOfWork.SaveChangesAsync();
                return Result.Successed("保存成功!");
            }
            catch (Exception ex)
            {
                _logger.LogError("保存友情链接异常", ex);
                throw;
            }
        }


        /// <summary>
        /// 设置友情链接是否可用
        /// </summary>
        /// <param name="Input"></param>
        /// <returns></returns>
        public async Task<Result> SetFriendlylinkEnabled(int id, bool enabled)
        {
            try
            {
                var friendlyLink = await _friendlyLinkRepository.GetFirstOrDefaultAsync(x => x.Id == id);
                if (friendlyLink == null) return null;
                friendlyLink.Enabled = enabled;
                await _friendlyLinkRepository.UpdateAsync(friendlyLink);
                await _unitOfWork.SaveChangesAsync();
                return Result.Successed("设置成功!");

            }
            catch (Exception ex)
            {
                _logger.LogError("设置友情链接是否可用异常", ex);
                throw;
            }
        }
    }
}
